import { MetricWeights, MatchPerformanceData } from '@/types/dota'

// 默认指标权重配置
export const DEFAULT_METRIC_WEIGHTS: MetricWeights = {
  hero_damage_per_min: 1.0,    // 英雄伤害/分钟 (HDPM)
  xpm: 0.8,                    // 经验/分钟 (XPM)
  gpm: 0.8,                    // 金钱/分钟 (GPM)
  vision_wards_per_min: 0.6,   // 真假眼/分钟 (VWPM)
  crowd_control_per_min: 0.7,  // 控制时间/分钟 (CCPM)
  healing_per_min: 0.4,        // 治疗/分钟 (HEPM)
  kill_participation: 1.2,     // 参战率 (KP%)
  deaths_per_min_penalty: 60   // 死亡/分钟惩罚系数
}

/**
 * 计算单场比赛的个人基础分 (Raw Score)
 * @param matchData 单场比赛数据
 * @param weights 指标权重配置
 * @param roleCoefficients 位置系数（可选，默认为1）
 * @returns 该场比赛的Raw Score
 */
export function calculateMatchRawScore(
  matchData: MatchPerformanceData,
  weights: MetricWeights = DEFAULT_METRIC_WEIGHTS
): number {
  const {
    hero_damage_per_min,
    vision_wards_per_min,
    crowd_control_per_min,
    healing_per_min,
    kill_participation,
    deaths_per_min,
    xpm,
    gpm
  } = matchData

  // 计算正向得分
  let positiveScore = 0
  positiveScore += hero_damage_per_min * weights.hero_damage_per_min
  positiveScore += xpm * weights.xpm
  positiveScore += gpm * weights.gpm
  positiveScore += vision_wards_per_min * weights.vision_wards_per_min
  positiveScore += crowd_control_per_min * weights.crowd_control_per_min
  positiveScore += healing_per_min * weights.healing_per_min
  positiveScore += kill_participation * weights.kill_participation

  // 应用位置系数（如果提供）
  // 这里可以根据英雄位置调整权重，暂时使用默认值1
  const roleCoef = 1.0

  positiveScore *= roleCoef

  // 计算惩罚项
  let penalty = 0
  penalty += deaths_per_min * weights.deaths_per_min_penalty

  // 最终Raw Score
  const rawScore = positiveScore - penalty

  return Math.max(0, rawScore) // 确保不为负数
}

/**
 * 计算玩家在联赛中的平均个人基础分 (Raw Score)
 * @param matches 玩家所有比赛数据
 * @param weights 指标权重配置
 * @returns 加权平均Raw Score
 */
export function calculatePlayerRawScore(
  matches: MatchPerformanceData[],
  weights: MetricWeights = DEFAULT_METRIC_WEIGHTS
): number {
  if (matches.length === 0) return 0

  let totalWeightedScore = 0
  let totalDuration = 0

  for (const match of matches) {
    const matchRawScore = calculateMatchRawScore(match, weights)
    const matchDuration = match.duration / 60 // 转换为分钟

    totalWeightedScore += matchRawScore * matchDuration
    totalDuration += matchDuration
  }

  return totalDuration > 0 ? totalWeightedScore / totalDuration : 0
}

/**
 * 计算最终评分
 * @param rawScore 个人基础分
 * @returns 最终评分
 */
export function calculateFinalScore(rawScore: number): number {
  return rawScore
}

/**
 * 处理单场比赛数据，计算各项指标
 * @param matchData 原始比赛数据
 * @param teamKills 团队总击杀数
 * @returns 处理后的比赛表现数据
 */
export function processMatchData(
  matchData: any,
  teamKills: number
): MatchPerformanceData {
  const duration = matchData.duration || 1800 // 默认30分钟，避免除零
  const durationMinutes = duration / 60

  return {
    match_id: matchData.match_id,
    duration: duration,
    win: matchData.win || false,
    
    // 原始数据
    kills: matchData.kills || 0,
    deaths: matchData.deaths || 0,
    assists: matchData.assists || 0,
    hero_damage: matchData.hero_damage || 0,
    xpm: matchData.xpm || 0,
    gpm: matchData.gpm || 0,
    observer_wards_placed: matchData.obs_placed || 0,
    sentry_wards_placed: matchData.sen_placed || 0,
    stuns: matchData.stuns || 0,
    hero_healing: matchData.hero_healing || 0,
    first_blood_claimed: matchData.firstblood_claimed || false,
    first_blood_given: matchData.firstblood_given || false,
    
    // 计算得出的指标
    hero_damage_per_min: (matchData.hero_damage || 0) / durationMinutes,
    vision_wards_per_min: ((matchData.obs_placed || 0) + (matchData.sen_placed || 0)) / durationMinutes,
    crowd_control_per_min: (matchData.stuns || 0) / durationMinutes,
    healing_per_min: (matchData.hero_healing || 0) / durationMinutes,
    kill_participation: teamKills > 0 ? ((matchData.kills || 0) + (matchData.assists || 0)) / teamKills : 0,
    deaths_per_min: (matchData.deaths || 0) / durationMinutes,
    
    // 初始化Raw Score为0，后续计算
    raw_score: 0,
    
    // 团队信息
    team_id: matchData.team_id,
    team_name: matchData.team_name,
    opponent_team_id: matchData.opponent_team_id,
    opponent_team_name: matchData.opponent_team_name
  }
}